package com.dao;


import com.entity.reader;
import com.entity.writer;
import com.utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class writerDao {
    public void add(writer userlogin) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into writerlogin(nickname,truename,idcard,password,email,sex,phone) values(?,?,?,?,?,?,?)";
        queryRunner.update(sql,userlogin.getNickname(),userlogin.getTruename(),userlogin.getIdcard(),userlogin.getPassword(),userlogin.getEmail(),userlogin.getSex(),userlogin.getPhone());
    }
    public writer findByPhone(String phone) throws SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from writerlogin where phone = ?";
        writer user = queryRunner.query(sql, new BeanHandler<>(writer.class),phone);
        return user;
    }

    public writer findByEmail(String email) throws SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from writerlogin where  email = ?";
        writer user = queryRunner.query(sql, new BeanHandler<>(writer.class),email);
        return user;
    }

    public writer findBynickname(String nickname) throws SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from writerlogin where  nickname = ?";
        writer user = queryRunner.query(sql, new BeanHandler<>(writer.class),nickname);
        return user;
    }

    public writer login(writer userman) throws  SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from writerlogin where ((phone = ? and password = ?) or (email = ? and password = ?))";
        writer entity = queryRunner.query(sql, new BeanHandler<>(writer.class), userman.getPhone(), userman.getPassword(),userman.getEmail(), userman.getPassword());
        return entity;
    }
}
